<?php

class KonsumenController extends Zend_Controller_Action
{
    private $_konsumen;
    
    public function init()
    {
        $this->_konsumen = new Application_Model_Konsumen();
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
        $this->view->konsumen = $this->_konsumen->getAllData();
    }

    public function addAction()
    {
        $form = new Application_Form_Konsumen();
        $this->view->form = $form;
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $data = array(
                    'nama_konsumen' => $form->getValue('nama_konsumen'),
                    'nama_perusahaan' => $form->getValue('nama_perusahaan'),
                    'alamat_konsumen' => $form->getValue('alamat_konsumen'),
                    'kota' => $form->getValue('kota'),
                    'provinsi' => $form->getValue('provinsi'),
                    'negara' => $form->getValue('negara'),
                    'kode_pos' => $form->getValue('kode_pos'),
                    'no_telepon' => $form->getValue('no_telepon'),
                    'fax' => $form->getValue('fax'),
                    'email' => $form->getValue('email')
                );
                
                $this->_konsumen->konsumenBaru($data);
                $this->_redirect('konsumen/index');
            } else {
                $form->populate($this->_request->getPost());
            }
        }
    }

    public function editAction()
    {
        $form = new Application_Form_Konsumen();
        $this->view->form = $form;
        
        if($this->_request->isPost())
        {
            if($form->isValid($this->_request->getPost()))
            {
                $data = array(
                    'nama_konsumen' => $form->getValue('nama_konsumen'),
                    'nama_perusahaan' => $form->getValue('nama_perusahaan'),
                    'alamat_konsumen' => $form->getValue('alamat_konsumen'),
                    'kota' => $form->getValue('kota'),
                    'provinsi' => $form->getValue('provinsi'),
                    'negara' => $form->getValue('negara'),
                    'kode_pos' => $form->getValue('kode_pos'),
                    'no_telepon' => $form->getValue('no_telepon'),
                    'fax' => $form->getValue('fax'),
                    'email' => $form->getValue('email')
                );
                
                $id_konsumen = $form->getValue('id_konsumen');
                
                $this->_konsumen->konsumenUpdate($data, $id_konsumen);
                $this->_redirect('konsumen/index');
            } else {
                $form->populate($this->_request->getPost());
            }
        } else {
            $id = $this->_getParam('id');
            $konsumen = $this->_konsumen->getDataKonsumen($id);
            foreach($konsumen as $konsumen);
            $form->populate($konsumen);
        }
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        $konsumen = $this->_konsumen->getDataKonsumen($id);
        foreach($konsumen as $konsumen);
        $this->view->konsumen = $konsumen;
    }


}









